package com.thinkfit.jy.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.thinkfit.jy.bean.EquipmentInfoBean;

public class EquipmentInfoUtils {

    private static EquipmentInfoUtils equipmentInfoUtils;

    private EquipmentInfoBean equipmentInfoBean;

    public static EquipmentInfoUtils getInstance(){
        if(equipmentInfoUtils == null){
            equipmentInfoUtils = new EquipmentInfoUtils();
        }
        return equipmentInfoUtils;
    }

    public EquipmentInfoBean getEquipmentInfoBean(){

        String equipmentInfo = SettingUtils.getInstance().getEquipmentInfo();
        if(!TextUtils.isEmpty(equipmentInfo)){
            equipmentInfoBean = GsonUtils.fromJson(equipmentInfo, EquipmentInfoBean.class);
        }else{
            equipmentInfoBean = new EquipmentInfoBean();
        }

        return equipmentInfoBean;
    }

    public void saveEquipmentInfoBean(String info){
        SettingUtils.getInstance().saveEquipmentInfo(info);
    }

    /**
     * 是否支持暂停功能
     * @return
     */
    public boolean isPause(){
        if(equipmentInfoBean != null){
            if(equipmentInfoBean.getIsPause() == 1){
                return true;
            }
        }
        return false;
    }

    public float getMaxSpeed(){
        if(equipmentInfoBean != null){
            return equipmentInfoBean.getResistance();
        }
        return 0f;
    }

    /**
     * 获取阻力/坡度
     * @return
     */
    public int getResistance(){
        if (!StringUtils.isEmpty(SettingUtils.getInstance().getEquipmentInfo())) {
            String equipmentInfo = SettingUtils.getInstance().getEquipmentInfo();
            equipmentInfoBean = GsonUtils.fromJson(equipmentInfo, EquipmentInfoBean.class);
        }else {
            return 0;
        }
        return equipmentInfoBean.getResistance();
    }

    public int getSlop(){
        if (!StringUtils.isEmpty(SettingUtils.getInstance().getEquipmentInfo())) {
            String equipmentInfo = SettingUtils.getInstance().getEquipmentInfo();
            equipmentInfoBean = GsonUtils.fromJson(equipmentInfo, EquipmentInfoBean.class);
        }else {
            return 0;
        }
        if(DeviceTypeUtils.getDeviceType() == 2){
            return equipmentInfoBean.getSlope();
        }else {
            return equipmentInfoBean.getResistance();
        }

    }

    public int getVersionCode(Context context) {
        PackageInfo packageInfo = getPackageInfo(context);
        return packageInfo != null ? packageInfo.versionCode : -1;
    }

    private PackageInfo getPackageInfo(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
